<template>
  <a-modal
    v-model.trim="showModal"
    title="企业信息"
    :mask-closable="false"
    @cancel="onCancel"
  >
    <a-form-model
      :model="departmentForm"
      ref="departmentForm"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      :rules="rules"
    >
      <a-form-model-item label="企业名称" prop="name">
        <a-input
          v-model.trim="departmentForm.name"
          :maxLength="20"
          placeholder="请输入企业名称"
        />
      </a-form-model-item>
    </a-form-model>
    <template #footer>
      <a-button @click="onCancel">取消</a-button>
      <a-button type="primary" @click="onSave">确定</a-button>
    </template>
  </a-modal>
</template>
<script>
import { updateEnterprise } from "@/services/user.js";
import Regex from "@/utils/regex.js";
import { storeQiankun } from 'if-qiankun-config';
const qianKunInstance = storeQiankun.Instance;
const originForm = {
  name: ""
};
export default {
  name: "departmentForm",
  data() {
    const nameCheckFun = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("名称不能为空"));
      } else if (!Regex.userName.test(value)) {
        callback(new Error("请输入汉字、字母、数字"));
      } else {
        callback();
      }
    };
    return {
      labelCol: { span: 5 },
      wrapperCol: { span: 14 },
      showModal: false,
      departmentForm: { ...originForm },
      rules: {
        name: [{ required: true, validator: nameCheckFun, trigger: "blur" }]
      }
    };
  },
  mounted() {
  },
  methods: {
    async show(data) {
      this.departmentForm = { ...data };
      this.showModal = true;
      this.$refs.departmentForm?.clearValidate();
    },
    onCancel() {
      this.showModal = false;
      this.departmentForm = { ...originForm };
    },
    // 保存
    async onSave() {
      if (!this.checkValid()) return;
      // 保存接口
      let params = {
        ...this.departmentForm
      };
      // 移除关键参数appid
      delete params.appId
      try {
        const {
          data: { message, code }
        } = await updateEnterprise(params);
        if (code === 200) {
          this.$message.success("保存成功");
          this.showModal = false;
          this.$emit("success");
          // 如果当前是非租户，修改这个的时候，也应该同步
          const currentState = qianKunInstance.state.value
          const { smallTenancyStatus, enterpriseList } = currentState
          if (smallTenancyStatus === 2) {
            enterpriseList[0].name = params.name
            Object.assign(qianKunInstance.state.value, {
              userName: params.name,
              enterpriseList
            });
            qianKunInstance.actions.setGlobalState(qianKunInstance.state);
          }
        } else {
          this.$message.error(message || "保存失败");
        }
      } catch (e) {
        this.showModal = false;
      }

    },
    checkValid() {
      let bol = false;
      this.$refs.departmentForm.validate((valid) => {
        bol = Boolean(valid);
      });
      return bol;
    }
  }
};
</script>
<style lang="less" scoped></style>
